package export.ldif;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.helper.Validate;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.text.WordUtils;


/**
 * Export CSV data to LDIF.<p>
 * 
 * Example: adding residents to ldap.<p>
 *
 */

public class LDIFUtility 
{

	public static void main(String... args) throws IOException
	{
		//AddResidents addRes = new AddResidents);
		
		//AddMDStudents addMDs = new AddMDStudents();
		
		//AddMasters addMasters = new AddMasters();

		//FindStaff findStaff = new FindStaff();
		
		AddStaff addStaff = new AddStaff();
		
	}//method


}//class


class AddResidents
{
	AddResidents() throws IOException
	{
		createAccounts();
		
	}//constructor


	AddResidents createAccounts() throws IOException
	{
		final List<String> lines = Files.readAllLines(
				Paths.get( "/home/khatfield/ldrive_res3.csv" ), 
				Charset.defaultCharset());
		
		final Iterator<String> linesIn = lines.iterator();
		
		Pattern regEx = Pattern.compile("(?<SurName>[^\\|]{0,})[\\|]{1}"+
                                  "(?<CommonName>[^\\|]{0,})[\\|]{1}"+
                                  "(?<Campus>[^\\|]{0,})[\\|]{1}"+
                                  "(?<Department>[^\\|]{0,})[\\|]{1}"+
                                  "(?<Type>[^\\|]{0,})[\\|]{1}"+
                                  "[^\\|]{0,}[\\|]{1}"+
                                  "[^\\|]{0,}[\\|]{1}"+
                                  "[^\\|]{0,}[\\|]{1}"+
                                  "[^\\|]{0,}");
		
		Integer ct = 20001001;
		
		StringBuilder ldifOut = new StringBuilder();
		
		while( linesIn.hasNext() )
		{
			String record = linesIn.next();
			
			Matcher regexMatcher = regEx.matcher(record);
			
			if( !regexMatcher.matches() )
			{
				System.err.println(record);
				System.exit(1);
				
			}
			else
			{
				ct++;
				
				ldifOut.append(	"dn: uid="+ct+","
								   +"ou=people,"
								   +"dc=musmlib,"
								   +"dc=org\n" );
				ldifOut.append(	"objectClass: inetOrgPerson\n" );
				ldifOut.append(	"objectClass: posixAccount\n" );
				ldifOut.append(	"objectClass: shadowAccount\n" );
				ldifOut.append(	"uid: " + ct + "\n" );
				ldifOut.append(	"sn: " + regexMatcher.group("SurName") 
						+ "\n" );
				ldifOut.append(	"cn: " + regexMatcher.group("CommonName") 
						+ "\n" );
				ldifOut.append(	"displayName: " 
						+ regexMatcher.group("CommonName") 
						+ " " 
						+ regexMatcher.group("SurName")  
						+ "\n" );
				ldifOut.append(	"uidNumber: " + ct + "\n" );
				ldifOut.append(	"gidNumber: 20000000\n" );
				ldifOut.append(	"homeDirectory: /bin/false\n" );				
				ldifOut.append(	"destinationIndicator: proxy-access\n" );
				ldifOut.append(	"l: " + regexMatcher.group("Campus") 
						+ "\n" );				
				ldifOut.append(	"registeredAddress: "
						+ regexMatcher.group("Department") 
						+ "\n" );				
				
				ldifOut.append(	"\n" );
				ldifOut.append(	"dn: cn=residents,"
						+"cn=faculty,"
						+"dc=musmlib,"
						+"dc=org"
						+"\n" );
				ldifOut.append(	"changetype: modify" + "\n" );
				ldifOut.append(	"add: uniquemember"  + "\n" );
				ldifOut.append(	"uniquemember: uid="+ct+","
						+"ou=people,"
						+"dc=musmlib,"
						+"dc=org" 
						+"\n\n" );	
				
			}//if
			
		}//while
		
		Files.write( Paths.get("/home/khatfield/add_residents_to_ldap.ldif"), 
				ldifOut.toString().getBytes() );
		
		System.out.println(ldifOut.toString());
		
		return this;
		
	}//method

}//class





class AddMDStudents
{
	AddMDStudents() throws IOException
	{
		createAccounts();
		
	}//constructor


	AddMDStudents createAccounts() throws IOException
	{
		final List<String> lines = Files.readAllLines(
				Paths.get( "/home/khatfield/ldrive.csv" ), 
				Charset.defaultCharset());
		
		final Iterator<String> linesIn = lines.iterator();
		
		Pattern regExMD = Pattern.compile("(?<SurName>[^\\|]{0,})[\\|]{1}"+
                                  "(?<CommonName>[^\\|]{0,})[\\|]{1}"+
                                  "(?<GivenName>[^\\|]{0,})[\\|]{1}"+
                                  "(?<Muid>[^\\|]{0,})[\\|]{1}"+
                                  "(?<Class>[^\\|]{0,})[\\|]{1}"+
                                  "(?<Email>[^\\|]{0,})[\\|]{1}"+
                                  "(?<Campus>[^\\|]{0,})[\\|]{1}"+                                  
                                  "[^\\|]{0,}[\\|]{1}"+
                                  "[^\\|]{0,}");
		
		StringBuilder ldifOut = new StringBuilder();
		
		Set<String> muidsAdded = new HashSet<>();

		Integer lineCt = 0;
		
		while( linesIn.hasNext() )
		{
			lineCt++;
			
			String record = linesIn.next();
			
			Matcher regexMatcher = regExMD.matcher(record);
			
			if( record.endsWith("STUDENTS") &&
							
			   !regexMatcher.matches() )
			{
				System.err.println("*** RECORD LAYOUT: "+lineCt+" ***");
				System.err.println(record);
				System.exit(1);
				
			}
			else if( record.endsWith("STUDENTS") )
			{
				String muid = regexMatcher.group("Muid").trim();
				
				if( muid.length() != 8 		|| 
					muid.matches("[^0-9]")  || 
					muidsAdded.contains(muid) )
				{
					System.err.println("*** MUID ***");
					
					if( muidsAdded.contains(muid) ) 
						System.err.println("*** DUPLICATED: "+lineCt+" ***");
					
					System.err.println(record);
					System.exit(1);
					
				}//if

				muidsAdded.add(muid);
				
				ldifOut.append(	"dn: uid="+ muid +","
								   +"ou=people,"
								   +"dc=musmlib,"
								   +"dc=org\n" );
				ldifOut.append(	"objectClass: inetOrgPerson\n" );
				ldifOut.append(	"objectClass: posixAccount\n" );
				ldifOut.append(	"objectClass: shadowAccount\n" );
				ldifOut.append(	"uid: " + muid + "\n" );
				
				ldifOut.append(	"sn: " + regexMatcher.group("SurName")
													 .trim() 
						+ "\n" );
				ldifOut.append(	"cn: " + regexMatcher.group("CommonName")
													 .trim() 
						+ "\n" );
				ldifOut.append(	"displayName: " 
						+ regexMatcher.group("CommonName") 
						+ " " 
						+ regexMatcher.group("SurName")  
						+ "\n" );
				
				if( regexMatcher.group("GivenName")
								.trim()
								.length() > 0 )
				{
					ldifOut.append(	"givenName: "  
						+ regexMatcher.group("GivenName")
									  .trim() 
						+ "\n" );
					
				}//if
				
				if( regexMatcher.group("Email")
								.trim()
								.length() > 3 )
				{
					ldifOut.append(	"mail: "  
							+ regexMatcher.group("Email") 
							+ "\n" );
					
				}//if
				
				ldifOut.append(	"uidNumber: " + muid + "\n" );
				ldifOut.append(	"gidNumber: 20000000\n" );
				ldifOut.append(	"homeDirectory: /bin/false\n" );				
				ldifOut.append(	"destinationIndicator: proxy-access\n" );
				
				ldifOut.append(	"l: " + regexMatcher.group("Campus")
													.trim() 
						+ "\n" );
				
				ldifOut.append(	"title: "
						+ regexMatcher.group("Class")
									  .replaceAll("[^0-9]", "") 
						+ "\n" );				
				
				ldifOut.append(	"\n" );
				
				ldifOut.append(	"dn: cn=med,cn=students,dc=musmlib,"
						+"dc=org"
						+"\n" );
				
				
				ldifOut.append(	"changetype: modify" + "\n" );
				ldifOut.append(	"add: uniquemember"  + "\n" );
				ldifOut.append(	"uniquemember: uid="+ muid +","
						+"ou=people,"
						+"dc=musmlib,"
						+"dc=org" 
						+"\n\n" );	
				
			}//if
			
		}//while
		
		Files.write( Paths.get("/home/khatfield/add_md_to_ldap.ldif"), 
				ldifOut.toString().getBytes() );
		
		System.out.println(ldifOut.toString());
		
		return this;
		
	}//method

}//class


class AddMasters
{
	AddMasters() throws IOException
	{
		createAccounts();
		
	}//constructor
	
	
	AddMasters createAccounts() throws IOException
	{
		final List<String> lines = Files.readAllLines(
				Paths.get( "/home/khatfield/ldrive.csv" ), 
				Charset.defaultCharset());
		
		final Iterator<String> linesIn = lines.iterator();
		
		/*
		 * Volcy|Sativa|10923879|Masters in Public Health|2012F||||MPH
		 * 
		 * Garrison|Andre|10784092|Macon NDCT (MFT)|2010S|2012S|Active/Not Attending||Masters Program Students
		 * 
		 * Erickson|Marc|10533817|Master of Science in Preclinical Sciences|2012F||Completed Prgm. But not Graduated||Masters Program Students
		 * 
		 * Daniels|Jonathan|10923065|Master of Sciences in Biomedical Sciences|2012F||||Masters Program Students
		 * 
		 * Chase|Brittany|10612560|Masters in Public Health|2007F|2009S|Active/Not Attending||Masters Program Students
		 * 
		 * McKenzie|Laura|10602112|Doctor of Philosophy - MED Macon|2012F|2017S|||Masters Program Students
		 * 
		 * Hood|Amanda|10174292|Master in Marriage and Family Therapy|2010F||Active/Not Attenting||Masters Program Students
		 */
		
		Pattern regExMD = Pattern.compile("(?<SurName>[^\\|]{0,})[\\|]{1}"+
                                  "(?<CommonName>[^\\|]{0,})[\\|]{1}"+
                                  "(?<Muid>[^\\|]{0,})[\\|]{1}"+
                                  "(?<Program>[^\\|]{0,})[\\|]{1}"+
                                  "(?<Class1>[^\\|]{0,})[\\|]{1}"+                                  
                                  "(?<Class2>[^\\|]{0,})[\\|]{1}"+
                                  "[^\\|]{0,}[\\|]{1}"+
                                  "[^\\|]{0,}[\\|]{1}"+
                                  "[^\\|]{0,}");
		
		StringBuilder ldifOut = new StringBuilder();
		
		Set<String> muidsAdded = new HashSet<>();

		Integer lineCt = 0;
		
		while( linesIn.hasNext() )
		{
			lineCt++;
			
			String record = linesIn.next();
			
			Matcher regexMatcher = regExMD.matcher(record);
			
			if( ( record.endsWith("Masters Program Students") || 
				  record.endsWith("MPH") ) 
				  
				  &&
							
			   !regexMatcher.matches() )
			{
				System.err.println("*** RECORD LAYOUT: "+lineCt+" ***");
				System.err.println(record);
				System.exit(1);
				
			}
			else if( record.endsWith("Masters Program Students") || 
					 record.endsWith("MPH") ) 
			{
				String muid = regexMatcher.group("Muid").trim();
				
				if( muid.length() != 8 		|| 
					muid.matches("[^0-9]")  || 
					muidsAdded.contains(muid) )
				{
					System.err.println("*** MUID: "+lineCt+" ***");
					
					if( muidsAdded.contains(muid) ) 
						System.err.println("*** DUPLICATED: "+lineCt+" ***");
					
					System.err.println(record);
					System.exit(1);
					
				}//if

				muidsAdded.add(muid);
				
				ldifOut.append(	"dn: uid="+ muid +","
								   +"ou=people,"
								   +"dc=musmlib,"
								   +"dc=org\n" );
				ldifOut.append(	"objectClass: inetOrgPerson\n" );
				ldifOut.append(	"objectClass: posixAccount\n" );
				ldifOut.append(	"objectClass: shadowAccount\n" );
				ldifOut.append(	"uid: " + muid + "\n" );
				
				ldifOut.append(	"sn: " + regexMatcher.group("SurName")
													 .trim() 
						+ "\n" );
				ldifOut.append(	"cn: " + regexMatcher.group("CommonName")
													 .trim() 
						+ "\n" );
				ldifOut.append(	"displayName: " 
						+ regexMatcher.group("CommonName")
									  .trim() 
						+ " " 
						+ regexMatcher.group("SurName")
									  .trim()  
						+ "\n" );
				
				
				ldifOut.append(	"uidNumber: " + muid + "\n" );
				ldifOut.append(	"gidNumber: 20000000\n" );
				ldifOut.append(	"homeDirectory: /bin/false\n" );				
				ldifOut.append(	"destinationIndicator: proxy-access\n" );				
				
				if( regexMatcher.group("Class2")
								.replaceAll("[^0-9]", "")
								.length() == 4 )
				{
					ldifOut.append(	"title: "
						+ regexMatcher.group("Class2")
									  .replaceAll("[^0-9]", "") 
						+ "\n" );
					
				}
				else if( regexMatcher.group("Class1")
									 .replaceAll("[^0-9]", "")
									 .length() == 4 )
				{
					ldifOut.append(	"title: "
							+ regexMatcher.group("Class1")
										  .replaceAll("[^0-9]", "") 
							+ "\n" );
					
				}
				else
				{
					System.err.println("*** CLASS: "+lineCt+" ***");
					
					System.err.println(record);
					System.exit(1);
					
				}//if
				
				ldifOut.append(	"\n" );
				
				String studentType = "";
				
				if( record.contains("MFT") || 
					record.contains("Family Therapy") ||
					record.contains("Family Services") )
				{
					studentType = "mft";
				
				}
				else if( record.contains("MPH") || 
						record.contains("Public Health") )
				{
						studentType = "mph";
					
				}
				else if( record.contains("Preclinical") )
				{
						studentType = "mspcs";
					
				}
				else if( record.contains("Biomedical") )
				{
						studentType = "msbms";
					
				}
				else if( record.contains("Philosophy") )
				{
						studentType = "phdpsy";
					
				}
				else if( record.contains("CRNA") || 
						 record.contains("MSA") )
				{
						studentType = "msa";
					
				}				
				else
				{
					System.err.println("*** STUDENT TYPE: "+lineCt+" ***");
					
					System.err.println(record);
					System.exit(1);
					
				}//if				
				
				ldifOut.append(	"dn: cn="+studentType+","
						+"cn=students,dc=musmlib,"
						+"dc=org"
						+"\n" );
				
				
				ldifOut.append(	"changetype: modify" + "\n" );
				ldifOut.append(	"add: uniquemember"  + "\n" );
				ldifOut.append(	"uniquemember: uid="+ muid +","
						+"ou=people,"
						+"dc=musmlib,"
						+"dc=org" 
						+"\n\n" );	
				
			}//if
			
		}//while
		
		Files.write( Paths.get("/home/khatfield/add_graduate_to_ldap.ldif"), 
				ldifOut.toString().getBytes() );
		
		System.out.println(ldifOut.toString());
				
		return this;
		
	}//method
	
}//class


class AddStaff
{
	
	/*
	 * Seeking: Ashley Jenkins
	 * name: Ashley L. Jenkins
	 * title: Medical Receptionist
	 * Phone 478-301-4111
	 * Email Address Clinard_AJ@Mercer.edu
	 * FAX 478-301-2387
	 * School/Division School of Medicine
	 * Department Mercer Medicine
	 * Home Address 4515 Irwinton Rd
	 * Home City Macon
	 * Home State GA
	 * Home Zip 31217
	 * Seeking: Mikata Jokkene
	 * name: Mikata S. Jokkene
	 * title: Clinical Skills Coordinator
	 * Phone 912-350-1724
	 * Email Address Jokkene_MS@Mercer.edu
	 * FAX 912-350-1766
	 * School/Division School of Medicine - Savannah
	 * Department Clinical Development & Assessment
	 * Seeking: Jessica Young
	 * name: Jessica Young: not found 
	 */

	AddStaff() throws IOException
	{
		//cleanAccountData();
		
		createAccounts();
		
	}//constructor
	
	
	AddStaff createAccounts() throws IOException
	{
		final List<String> lines = Files.readAllLines(
				Paths.get( "/home/khatfield/staffDataCleanedSorted.csv" ), 
				Charset.defaultCharset());

		final Iterator<String> linesIn = lines.iterator();

		/*
		 * Thompson|Betty||thompson_b@mercer.edu|478.301.5806|CMA: Medical Assistant|Mercer Medicine|MCN
		 */

		Pattern regExMD = Pattern.compile("(?<SurName>[^\\|]{0,})[\\|]{1}"+
				"(?<CommonName>[^\\|]{0,})[\\|]{1}"+
				"(?<GivenName>[^\\|]{0,})[\\|]{1}"+				
				"(?<Email>[^\\|]{0,})[\\|]{1}"+
				"(?<Phone>[^\\|]{0,})[\\|]{1}"+
				"(?<Title>[^\\|]{0,})[\\|]{1}"+                                  
				"(?<Dept>[^\\|]{0,})[\\|]{1}"+
				"(?<Campus>[^\\|]{0,})");

		StringBuilder ldifOut = new StringBuilder();

		Integer lineCt = 0;

		Integer uid = 30000000;
		
		while( linesIn.hasNext() )
		{
			lineCt++;

			String record = linesIn.next();

			Matcher regexMatcher = regExMD.matcher(record);

			if( !regexMatcher.matches() )
			{
				System.err.println("*** RECORD LAYOUT: "+lineCt+" ***");
				System.err.println(record);
				System.exit(1);

			}
			else 
			{
				ldifOut.append(	"dn: uid="+ ++uid +","
						+"ou=people,"
						+"dc=musmlib,"
						+"dc=org\n" );
				ldifOut.append(	"objectClass: inetOrgPerson\n" );
				ldifOut.append(	"objectClass: posixAccount\n" );
				ldifOut.append(	"objectClass: shadowAccount\n" );
				ldifOut.append(	"uid: " + uid + "\n" );

				ldifOut.append(	"sn: " + regexMatcher.group("SurName")
						.trim() 
						+ "\n" );
				ldifOut.append(	"cn: " + regexMatcher.group("CommonName")
						.trim() 
						+ "\n" );
				ldifOut.append(	"displayName: " 
						+ regexMatcher.group("CommonName")
						.trim() 
						+ " " 
						+ regexMatcher.group("SurName")
						.trim()  
						+ "\n" );

				if( regexMatcher.group("GivenName")
						.trim()
						.length() > 0 )
				{
					ldifOut.append(	"givenName: "  
							+ regexMatcher.group("GivenName")
							.trim() 
							+ "\n" );

				}//if

				if( regexMatcher.group("Email")
						.trim()
						.length() > 3 )
				{
					ldifOut.append(	"mail: "  
							+ regexMatcher.group("Email") 
							+ "\n" );

				}//if
				
				if( regexMatcher.group("Phone")
						.trim()
						.length() > 3 )
				{
					ldifOut.append(	"telephoneNumber: "  
							+ regexMatcher.group("Phone") 
							+ "\n" );

				}//if
				
				ldifOut.append(	"uidNumber: " + uid + "\n" );
				ldifOut.append(	"gidNumber: 20000000\n" );
				ldifOut.append(	"homeDirectory: /bin/false\n" );				
				ldifOut.append(	"destinationIndicator: proxy-access\n" );				

				String campus = "";
				
				if( regexMatcher.group("Campus").contains("SAV") )
					campus = "Savannah";

				if( regexMatcher.group("Campus").contains("COL") )
					campus = "Columbus";

				if( regexMatcher.group("Campus").contains("MCN") )
					campus = "Macon";
				
				ldifOut.append(	"l: " + campus 
						+ "\n" );				
				ldifOut.append(	"registeredAddress: "
						+ regexMatcher.group("Dept") 
						+ "\n" );				
								
				ldifOut.append(	"\n" );

				ldifOut.append(	"dn: cn=staff,"
						+"dc=musmlib,"
						+"dc=org"
						+"\n" );

				ldifOut.append(	"changetype: modify" + "\n" );
				ldifOut.append(	"add: uniquemember"  + "\n" );
				ldifOut.append(	"uniquemember: uid="+ uid +","
						+"ou=people,"
						+"dc=musmlib,"
						+"dc=org" 
						+"\n\n" );	

			}//if

		}//while

		Files.write( Paths.get("/home/khatfield/add_staff_to_ldap.ldif"), 
				ldifOut.toString().getBytes() );

		System.out.println(ldifOut.toString());

		return this;
			
	}//method
	
	
	AddStaff cleanAccountData() throws IOException
	{
		final List<String> lines = Files.readAllLines(
				Paths.get( "/home/khatfield/staffData.csv" ), 
				Charset.defaultCharset());
		
		final Iterator<String> linesIn = lines.iterator();
		
		Pattern namePtn = Pattern.compile("^name:\\s[a-zA-Z\\s\\.\\-',]{1,}");
		
		Pattern titlePtn = Pattern.compile(
				"^title:\\s(?<Title>[\\w\\W]{1,})");

		Pattern phonePtn = Pattern.compile(
				"Phone\\s(?<Phone>[\\s0-9\\-\\.]{6,})");
		
		Pattern emailPtn = Pattern.compile(
				"Email\\sAddress\\s(?<Email>[\\w\\W]{6,})");
		
		Pattern deptPtn = Pattern.compile(
				"Department\\s(?<Dept>[\\w\\W]{2,})");
		
		StringBuilder results = new StringBuilder();
		
		while( linesIn.hasNext() )
		{
			String rec = linesIn.next();
			
			if( namePtn.matcher(rec).lookingAt() 
					&& 
				!rec.contains("not found") )
			{
				String lname = "";
				String mname = "";
				String fname = "";
				
				if( rec.trim()
					   .split("\\s")
					   .length == 4 )
				{
					lname = rec.trim().split("\\s")[3]; 
					mname = rec.trim().split("\\s")[2];
					fname = rec.trim().split("\\s")[1];
					
				}
				else
				{
					lname = rec.trim().split("\\s")[2]; 
					fname = rec.trim().split("\\s")[1];
					
				}//if
				
				results.append("\n" + lname + "|" 
									+ fname + "|" 
									+ mname + "|");
				
				String title = "";
				String phone = "";
				String email = "";
				String dept  = "";
				
				while( linesIn.hasNext() )
				{
					String subRec = linesIn.next();
					
					Matcher titleMtc = titlePtn.matcher(subRec); 
					
					if( titleMtc.lookingAt() )
					{
						title = titleMtc.group("Title");
						
					}//if

					Matcher phoneMtc = phonePtn.matcher(subRec); 

					if( phoneMtc.lookingAt() )
					{
						phone = phoneMtc.group("Phone");
						
					}//if
					
					Matcher emailMtc = emailPtn.matcher(subRec);
					
					if( emailMtc.lookingAt() )
					{
						email = emailMtc.group("Email");

					}//if

					Matcher deptMtc = deptPtn.matcher(subRec);
					
					if( deptMtc.lookingAt() )
					{
						dept = deptMtc.group("Dept");

						break;
						
					}//if
					
				}//while

				results.append( email.toLowerCase() + "|" );
				
				results.append( phone.replace('-', '.') + "|" );
				
				results.append( title + "|" );
				
				results.append( dept + "|" );
				
				String campus = "";
				
				if( phone.startsWith("912") ) campus = "SAV";
				else if( phone.startsWith("478-301-") || 
						 phone.startsWith("478-633-") ) campus = "MCN";
				else if( phone.startsWith("678") ) campus = "COL";
				else
				{
					System.err.print(phone + "\n" + results.toString());
					System.exit(1);
					
				}
				
				results.append( campus );
				
			}//if
			
		}//while

		Files.write(Paths.get("/home/khatfield/staffDataCleaned.csv"),
				results.toString().getBytes() );

		return this;
		
	}//method
	
}//class


/**
 * Search mercer.edu directory and retrieve results.<pr>
 *
 * Search a list of names, recording information found.<pr>
 */

class FindStaff
{
	FindStaff() throws IOException
	{
		seekAccounts();
		
	}//constructor
	
	
	void seekAccounts() throws IOException
	{
		StringBuilder accountDataFound = new StringBuilder();
		
		final List<String> names = Files.readAllLines(Paths.get(
				"/home/khatfield/staff"), 
				Charset.defaultCharset());
	
		final Iterator<String> namesList = names.iterator();
		
		Pattern staffRegEx = Pattern.compile("(?<SurName>[^\\|]{0,})[\\|]{1}"+
                                  "(?<CommonName>[^\\|]{0,})[\\s|\\t|\\|]{1}"+
                                  "[^\\|]{0,}[\\|]{1}"+
                                  "[^\\|]{0,}[\\|]{1}"+
                                  "[^\\|]{0,}[\\|]{1}"+
                                  "[^\\|]{0,}[\\|]{1}"+
                                  "[^\\|]{0,}[\\|]{1}"+
                                  "[^\\|]{0,}[\\|]{1}"+
                                  "[^\\|]{0,}");
		
		while( namesList.hasNext() )
		{
			String lineIn = WordUtils.capitalize( namesList.next()
					.toLowerCase(),'|');

			Matcher staffRegExMtchr = staffRegEx.matcher(lineIn);
			
			if( !staffRegExMtchr.matches() )
			{
				accountDataFound.append( "RECORD\n" + lineIn + "\n" );
				break;
				
			}//if
			
			String nameSearch = staffRegExMtchr.group("CommonName")
											   .split("\\s")[0] + " " + 
					staffRegExMtchr.group("SurName");

			accountDataFound.append( "Seeking: " + nameSearch + "\n" );			
			
			Document doc = Jsoup.connect(
					"http://apps.mercer.edu/directory/index.cfm")
					  .data("txtSearch", nameSearch)
					  .data("optClassify","faculty-staff")
					  .referrer("http://mercer.edu")
					  .userAgent("Safari 4.0 on Mac OS X")
					  .timeout(5000)
					  .post();		

			accountDataFound.append( "name: " );
			
			String namesFnd = searchHTMLByTag(doc,"h2");
			
			if( namesFnd.trim().length() < 2 )
			{
				accountDataFound.append( nameSearch + ": not found\n" );
				
			}
			else
			{
				accountDataFound.append( namesFnd );
				
				accountDataFound.append( "title: " );
				
				accountDataFound.append( searchHTMLByTag(doc,"h3") );
				
				accountDataFound.append( searchHTMLByAttr(doc, "class", 
						"excel-like") );
				
			}//if			
			
		}//while

		
		Files.write( Paths.get("/home/khatfield/staffData.csv"), 
				accountDataFound.toString().getBytes() );
		
	}//method

	
	String searchHTMLByTag( final Document docIn, final String tag )
	{
		final List<Element> nameTags = docIn.getElementsByTag(tag);
		
		final Iterator<Element> nameTagsList = nameTags.iterator();
		
		String results = "";
		
		while( nameTagsList.hasNext() )
		{
			results += nameTagsList.next()
								   .text();

			results += "\n";
			
		}//while
		
		return results;
		
	}//method
	
	String searchHTMLByAttr( final Document docIn, 
							 final String attrID, 
							 final String attrVal )
	{
		final List<Element> nameTags = docIn.getElementsByAttributeValue(
				attrID, attrVal);
		
		final Iterator<Element> nameTagsList = nameTags.iterator();
		
		String results = "";
		
		while( nameTagsList.hasNext() )
		{
			List<Element> rws = nameTagsList.next().getElementsByTag("tr");
			
			Iterator<Element> rwsList = rws.iterator(); 
			
			while( rwsList.hasNext() )
			{
				results += rwsList.next().text();

				results += "\n";
				
			}//while
			
		}//while
		
		return results;
		
	}//method
	
}//class
